﻿using IMark.DAO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IMark.Admin.EmailMarketing
{
    public partial class Send : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Utils.CheckAuthentication())
            {
                if (!IsPostBack)
                {
                    LoadData();
                }
            }
        }

        private void LoadData()
        {
            StringBuilder str = new StringBuilder();

            List<SAN_PHAM> lstProductData = SanPhamModels.GetAll();
            foreach (var item in lstProductData)
            {
                HINH_SAN_PHAM hinhSanPham = HinhSanPhamModels.GetTop1ByIDProduct(item.ID);
                string urlImg = ResolveClientUrl("~/images/products/" + hinhSanPham.Hinh);
                str.Append("<tr class=\"odd gradePhone\" id=\"ProductAddID_"+item.ID+"\">");
                str.Append("<td>");
                str.Append("<img style=\"width: 350px; max-height: 200px;\" src=\""+urlImg+"\" />");
                str.Append("</td>");
                str.Append("<td style=\"width: 250px;\">");
                str.Append("<div class=\"name\"><a href=\"#\">"+item.TenSanPham+"</a></div>");
                str.Append("<div class=\"small-price\">"+item.DonGia.ToString("###,###")+"</div>");
                str.Append("<button onclick=\"addListIDProduct("+item.ID+")\" class=\"btn btn-primary\"><i class=\"fa fa-plus fa-fw\"></i></button>");
                str.Append("</td>");
                str.Append("</tr>");
            }
            lstProduct.InnerHtml = str.ToString();
        }

        protected void btnSend_Click(object sender, EventArgs e)
        {
            if (txtEmailTitle.Value != "" && txtContent.Value != "")
            {
                List<DANG_KY_NHAN_MAIL> lstMail = DangKyNhanMailModels.GetListSend();
                if(lstMail.Count > 0)
                {
                    foreach (var item in lstMail)
                    {
                        Utils.SendGMail(item.Email, txtContent.Value, txtEmailTitle.Value);
                    }
                    info.InnerHtml = "<div class='alert alert-success'>Gửi mail tới khách hàng thành công !!!</div>";
                }
                else
                {
                    info.InnerHtml = "<div class='alert alert-danger'>Chưa có khách hàng đăng ký nhận Mail,Hệ thống chưa có mail khách hàng để gửi.</div>";
                }
            }
            else
            {
                info.InnerHtml = "<div class='alert alert-danger'>Vui lòng nhập đủ thông tin !!!</div>";
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/Account/Home.aspx");
        }
    }
}